package com.xing.gulimall.product.service.impl;

import com.xing.common.constant.ProductConstant;
import com.xing.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.xing.gulimall.product.entity.AttrEntity;
import com.xing.gulimall.product.service.AttrAttrgroupRelationService;
import com.xing.gulimall.product.service.AttrService;
import com.xing.gulimall.product.vo.AttrGroupRelationVo;
import com.xing.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.common.utils.PageUtils;
import com.xing.common.utils.Query;

import com.xing.gulimall.product.dao.AttrGroupDao;
import com.xing.gulimall.product.entity.AttrGroupEntity;
import com.xing.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrService attrService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 分页查询分类属性分组
     *
     * @param params
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();


        if (!StringUtils.isEmpty(key)) {
            wrapper.and(obj -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);

        }
    }

    /**
     * 用attrGroup的同一个分类的其他组，在中间表中没有与attrGroup关联的数据
     *
     * @param params
     * @param attrGroupId
     * @return
     */
    @Override
    public PageUtils NoAttrRelation(Map<String, Object> params, Long attrGroupId) {
        //attrgroup查出 分类ID
        AttrGroupEntity attrGroupEntity = this.baseMapper.selectById(attrGroupId);
        Long catelogId = attrGroupEntity.getCatelogId();

        //查出当前分类的其它分组GroupId
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id", catelogId);
        List<AttrGroupEntity> attrGroupEntitieList = this.baseMapper.selectList(queryWrapper);


        List<Long> attGroupIds = attrGroupEntitieList.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());

        //找中间表，查出attrid
        QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("attr_group_id", attGroupIds);

        List<AttrAttrgroupRelationEntity> relationEntityList = attrAttrgroupRelationService.list(queryWrapper1);

        List<Long> attrIds = relationEntityList.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

        //找attr查出
        QueryWrapper<AttrEntity> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("catelog_id", catelogId)
                //只查基本属性
                .eq("attr_type", ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());

        if (attrIds != null && attrIds.size() > 0) {
            queryWrapper2.notIn("attr_id", attrIds);
        }

        //模糊查询
        String key = (String) params.get("key");

        if (StringUtils.isNotEmpty(key)) {
            queryWrapper2.and(((obj) -> {
                obj.eq("attr_id", key).or().like("attr_name", key);
            }));
        }
        IPage<AttrEntity> pa = new Query<AttrEntity>().getPage(params);
        IPage<AttrEntity> page = attrService.page(pa, queryWrapper2);
        return new PageUtils(page);
    }

    /**
     * 保存关联关系
     *
     * @param attrGroupRelationVos
     */
    @Override
    public void saveRelation(AttrGroupRelationVo... attrGroupRelationVos) {
        List<AttrGroupRelationVo> attrGroupRelationVo = Arrays.asList(attrGroupRelationVos);
        List<AttrAttrgroupRelationEntity> collect = attrGroupRelationVo.stream().map((attrGroupRelationVo1) -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(attrGroupRelationVo1, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationService.saveOrUpdateBatch(collect);
    }


    /**
     * 获取分类下的所有分组和属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1.查询分组信息
        List<AttrGroupEntity> catelog_id = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //2.查询所有属性
        List<AttrGroupWithAttrsVo> collect = catelog_id.stream().map((group) -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrsVo);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(relationAttr);
            return attrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

}